//
//  ViewController.m
//  省市
//
//  Copyright © 2016年 zph. All rights reserved.
//

#import "ViewController.h"
#import "CZcity.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
/**
 *  存放数据的数组
 */
@property (nonatomic,strong)NSArray *citiesArray;
/**
 *  pickerview
 */
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
/**
 *  省文本
 */
@property (weak, nonatomic) IBOutlet UILabel *proLabel;
/**
 *  城市文本
 */
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;
/**
 *  省模型
 */
@property (nonatomic,strong)CZcity * selecletPro;

@end

@implementation ViewController
/**
 *  懒加载
 */
-(NSArray *)citiesArray{
    if (!_citiesArray) {
        NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"02cities" ofType:@"plist"]];
        NSMutableArray *nmArray = [NSMutableArray arrayWithCapacity:array.count];
        for (NSDictionary *dic in array) {
            [nmArray addObject:[CZcity citiesWithDic:dic]];
        }
        _citiesArray = nmArray;
    }
    return _citiesArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self pickerView:self.pickerView didSelectRow:0 inComponent:0];
    
}

#pragma mark --uipickerviewdatasource中的方法
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (component == 0) {
        return self.citiesArray.count;
    }else{
        
        // 2.如果是第1组，城市 城市的行数就要看当前正在显示的是哪个省
        //获取省的索引
        NSInteger seleProIndx = [pickerView selectedRowInComponent:0];
        //获取当前省的数据
        CZcity * selePro = self.citiesArray[seleProIndx];
        //保存当前省的数据
        self.selecletPro = selePro;
        //返回省的城市的个数
        return selePro.cities.count;

    }
}
//返回每一行
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        
        //返回省的名字
        CZcity * city = self.citiesArray[row];
        return city.name;
    }else{
//        //返回城市的名字
//        //获取省的索引
//        NSInteger seleProIndx = [pickerView selectedRowInComponent:0];
//        //获取当前省的数据
//        CZcity * selePro = self.citiesArray[seleProIndx];
//        //返回省的城市的名字
//        return selePro.cities[row];
        //返回保存的省里面的内容
        return self.selecletPro.cities[row];
    }

}
//当用户选中某一行是刷新
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        [pickerView reloadComponent:1];
    }
    
    //设置label的内容
    //选择了第几组的行
    NSInteger selePro = [pickerView selectedRowInComponent:0];
    NSInteger seleCity = [pickerView selectedRowInComponent:1];
    
    //获取内容
    CZcity * pro = self.citiesArray[selePro];
//    NSString * city = pro.cities[seleCity];
    //获取保存的省里面的城市
    NSString * city = self.selecletPro.cities[seleCity];
    //赋值
    self.proLabel.text = pro.name;
    self.cityLabel.text = city;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
